package com.xunua.networkproject.RxJavaAndButterKnife;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.xunua.networkproject.Constant;
import com.xunua.networkproject.R;
import com.xunua.networkproject.Retrofit.RetrofitService;
import com.xunua.networkproject.RetrofitBean.PostFileResultBean;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RxJava_ButterActivity extends AppCompatActivity {
    private static final String TAG = "RxJava_ButterActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_java__butter);
        ButterKnife.bind(this);//必须在setContentView之后
    }

    @OnClick(R.id.Ob_btn_01)
    void Ob(){
        //Observer 接口
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(TAG, "onSubscribe: " +d);
            }

            @Override
            public void onNext(@NonNull String s) {// onNext() （相当于 onClick() / onEvent()）
                Log.e(TAG, "onNext: "+s);
            }

            @Override
            public void onError(@NonNull Throwable e) {//事件队列异常。
                Log.e(TAG, "onError: " +e);
            }

            @Override
            public void onComplete() {//onCompleted(): 事件队列完结。
                Log.e(TAG, "onComplete: " );
            }
        };

        //创造被观察者
        @NonNull Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) {
                emitter.onNext("Hello next01");
                emitter.onNext("Hello next02");
                emitter.onNext("Hello next03");
                emitter.onComplete();
            }
        });
        observable.subscribe(observer);
    }
    @OnClick(R.id.Ob_btn_02)
    void Ob02(){
        Observable.just("Hello world!").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) {
                Log.e(TAG, "accept: " + s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }
    @OnClick(R.id.RxJava_btn_01)//OkHttp+Retrofit+RxJava联用
    void RxJavaMap(){
        OkHttpClient client = new OkHttpClient.Builder()
//                .addInterceptor()//可以自己添加拦截器
                .connectTimeout(1000, TimeUnit.MILLISECONDS)//定义超时
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(Constant.baseUrl_wifi)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        retrofit.create(RxJavaService.class).getText()// IO 线程，由 subscribeOn() 指定
                .doOnNext(new Consumer<RxJavaResultBean>() {
                    @Override
                    public void accept(RxJavaResultBean resultBean) throws Exception {
                        resultBean.setMessage("doOnnNExt");
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .map(new Function<RxJavaResultBean, RxJavaResultBean>() {//在新创建的线程执行了
                    @Override
                    public RxJavaResultBean apply(RxJavaResultBean resultBean) throws Exception {
                        resultBean.setMessage(resultBean.getMessage()+"------------\n"+Thread.currentThread().getName()+":这里被我在RxJava修改过了---------");
                        return resultBean;
                    }
                })
                .observeOn(Schedulers.io())
                .map(new Function<RxJavaResultBean, RxJavaResultBean>() {
                    @Override
                    public RxJavaResultBean apply(RxJavaResultBean resultBean) throws Exception {
                        resultBean.setMessage(resultBean.getMessage()+"\n------第二次修改:"+Thread.currentThread().getName());
                        return resultBean;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RxJavaResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e(TAG, "onSubscribe: "+d.isDisposed() );
                    }

                    @Override
                    public void onNext(RxJavaResultBean resultBean) {
                        Toast.makeText(RxJava_ButterActivity.this, "请求成功：\n"+resultBean.getMessage(), Toast.LENGTH_LONG).show();
                        Log.e(TAG, "onNext: "+resultBean.toString() );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(RxJava_ButterActivity.this, "请求失败了", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "onError: "+e );
                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: " );
                    }
                });
    }

    @OnClick(R.id.RxJava_btn_02)
    void RxJava02(){
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(1000, TimeUnit.MILLISECONDS)//定义超时连接
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(Constant.baseUrl_wifi)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        retrofit.create(RxJavaService.class).getText()
                .subscribeOn(Schedulers.io())
                .flatMap(new Function<RxJavaResultBean, Observable<PostFileResultBean>>() {
                    @Override
                    public Observable<PostFileResultBean> apply(RxJavaResultBean resultBean) throws Exception {
                        return retrofit.create(RxJavaService.class).postLogin("嗨咯","你很棒");

                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PostFileResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PostFileResultBean postFileResultBean) {
                        Toast.makeText(RxJava_ButterActivity.this, "请求成功\n"+postFileResultBean.getMessage().substring(11), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(RxJava_ButterActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}

/**

 new io.reactivex.rxjava3.core.Observer<String>() {
@Override
public void onSubscribe(io.reactivex.rxjava3.disposables.@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
Log.e(TAG, "onSubscribe: "+d.isDisposed() );
}

@Override
public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
Log.e(TAG, "onNext: "+s );
}

@Override
public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
Log.e(TAG, "onError: "+e );
}

@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
}


 */
